package com.berwin.apkdistribution.view;

import com.sun.tools.javac.comp.Flow;

import javax.swing.*;
import java.awt.*;

public class AttributeDialog extends JDialog {

    private String key;
    private String value;

    private JTextField tfKey;
    private JTextField tfValue;

    private OnAttributeSaveClicked onAttributeSaveClicked;

    public static class OnAttributeSaveClicked {
        void onClicked(String key, String value) {
        }
    }

    public AttributeDialog(JFrame owner, String title, String key, String value, OnAttributeSaveClicked onAttributeSaveClicked) {
        super(owner, title, true);
        this.key = key;
        this.value = value;
        this.onAttributeSaveClicked = onAttributeSaveClicked;
        this.setSize(new Dimension(310, 100));
        this.setLayout(new BorderLayout());
        this.add(this.initCenter(), BorderLayout.CENTER);
        this.add(this.initBottom(), BorderLayout.SOUTH);
        this.setLocationRelativeTo(null);
    }

    void setKeyEditable(boolean editable) {
        this.tfKey.setEnabled(editable);
        this.tfKey.setEditable(editable);
    }

    private JPanel initCenter() {
        JPanel pnlCenter = new JPanel();
        pnlCenter.setLayout(new FlowLayout(FlowLayout.LEFT));

        this.tfKey = new JTextField(11);
        pnlCenter.add(this.tfKey);
        this.tfKey.setText(this.key);

        pnlCenter.add(new JLabel(":"));

        this.tfValue = new JTextField(11);
        pnlCenter.add(tfValue);
        this.tfValue.setText(this.value);

        return pnlCenter;
    }

    private JPanel initBottom() {
        JPanel pnlBottom = new JPanel();
        pnlBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton btnCancel = new JButton("取消");
        pnlBottom.add(btnCancel);
        btnCancel.addActionListener(e -> {
            this.dispose();
        });

        JButton btnSave = new JButton("保存");
        pnlBottom.add(btnSave);
        btnSave.addActionListener(e -> {
            this.key = this.tfKey.getText();
            this.value = this.tfValue.getText();
            this.onAttributeSaveClicked.onClicked(this.key, this.value);
            this.dispose();
        });
        this.getRootPane().setDefaultButton(btnSave);

        return pnlBottom;
    }


}
